package com.mi.minal.module.system.controller;

import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.mi.minal.common.util.BaseService;
import com.mi.minal.module.system.service.SystemService;
import com.mi.minal.mybatis.entity.SysDict;
import com.mi.minal.mybatis.entity.SysMenu;
import com.mi.minal.mybatis.entity.SysResource;
import com.mi.minal.mybatis.entity.SysRole;
import com.mi.rule.auth.Public;
import com.mi.rule.resource.ResC;
import com.mi.rule.resource.ResM;
import com.mi.rule.util.MathKit;
import com.mi.rule.util.project.SRS;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@ResC("系统管理")
@RestController
@RequestMapping("/system")
public class SystemController implements BaseService {
    @Autowired
    SystemService systemService;

    @Public
    @ResM("读取资源树")
    @RequestMapping("/resource/tree")
    public SRS resourceTree(@RequestBody SRS param) {
        SysDict sysDict = new SysDict();
        sysDict.setValue("555");
        sysDict.setCode("555");
        systemService.insertDict(sysDict);
        List<TableInfo> tableInfos = TableInfoHelper.getTableInfos();
//        MiFillHandler miFillHandler = FillHelp.fillHandler();
//        HashMap<String, Object> insertFill = new HashMap<>();
//        HashMap<String, Object> updateFill = new HashMap<>();
//        miFillHandler.insertFill(insertFill);
//        miFillHandler.updateFill(updateFill);


        List<SysRole> roles = systemService.roles();
        List<SysMenu> menus = systemService.menus();
        List<SysResource> resources = systemService.resources();
        List<String> strings = systemService.roleMenuUrls(1);
//        List<SysMenu> sysMenus = systemService.roleMenuList2(1);
//        List<String> strings1 = systemService.roleResourceUrls2("1");
        List<SysResource> sysResources = systemService.roleResourceList("1");

        return SRS.bySuccess();
    }

    @Public
    @ResM("刷新角色资源缓存")
    @RequestMapping("/refresh/roleResource")
    public SRS refreshRoleResource(@RequestBody SRS param) {
        return systemService.systemResourceCleanAdd(param);
    }

    //传递账号密码，返回加密结果
    //参数【account-明文账号，password-明文密码】
    //返回【account-明文账号，password-明文密码，frontPassword-管理后台传递过来的密码，dbPassword-数据库中的密码】
    @Public
    @RequestMapping("/user/772060BD5C48F2D1B21DAD8C2772FAAA/772060BD5C48F2D1B21DAD8C2772FAAA")
    public SRS resourceRoleMenu(@RequestBody SRS param) {
        String account = param.getString("account");
        String password = param.getString("password");
        String frontPassword = MathKit.getMD5(account + password);
        String dbPassword = addSalt(frontPassword);
        param.set("frontPassword", frontPassword);
        param.set("dbPassword", dbPassword);
        return SRS.bySuccess(param);
    }


}
